class WordsController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @title1 = 'Dictionary'
    @url1 = '/words/'
    @word_pages, @words = paginate :words, :per_page => 25
  end

  def show
    @title1 = 'Dictionary'
    @url1 = '/words/'
    @word = Word.find(params[:id])
    @title2 = @word.name
    @url2 = "/words/show/#{params[:id]}"
  end

  def new
    @title1 = 'Dictionary'
    @url1 = '/words/'
    @title2 = 'New'
    @url2 = '/words/new'
    @word = Word.new
  end
  
  def new_batch
    @title1 = 'Dictionary'
    @url1 = '/words/'
    @title2 = 'New batch'
    @url2 = '/words/new_batch'
  end

  def create
    @word = Word.new(params[:word])
    if @word.save
      flash[:notice] = 'Word was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end
  
  def create_batch
    words = params[:words].split
    words.each { |w|
      Word.create(:name => w)
    }
    flash[:notice] = 'Words added successfully.'
    redirect_to :action => 'list'
  end

  def edit
    @title1 = 'Dictionary'
    @url1 = '/words/'
    @word = Word.find(params[:id])
    @title2 = "Edit #{@word.name}"
    @url2 = "/words/edit/#{params[:id]}"
  end

  def update
    @word = Word.find(params[:id])
    if @word.update_attributes(params[:word])
      flash[:notice] = 'Word was successfully updated.'
      redirect_to :action => 'show', :id => @word
    else
      render :action => 'edit'
    end
  end

  def destroy
    Word.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
